//package com.zzyl.nursing.service.impl;
//
//import java.awt.print.Pageable;
//import java.time.Instant;
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.util.List;
//
//import cn.hutool.core.bean.BeanUtil;
//import cn.hutool.core.date.DateUtil;
//import cn.hutool.core.date.LocalDateTimeUtil;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.zzyl.common.core.page.TableDataInfo;
//import com.zzyl.common.utils.UserThreadLocal;
//import com.zzyl.nursing.dto.ReservationDto;
//import com.zzyl.nursing.vo.TimeCountVo;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import com.zzyl.nursing.mapper.ReservationMapper;
//import com.zzyl.nursing.domain.Reservation;
//import com.zzyl.nursing.service.IReservationService;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import cn.hutool.core.util.ObjUtil;
//import com.zzyl.common.exception.base.BaseException;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.zzyl.common.constant.HttpStatus;
//import com.zzyl.common.exception.ServiceException;
//
//
//import java.util.Map;
//
///**
// * 预约信息Service业务层处理
// *
// * @author zouguanghuan
// * @date 2025-10-28
// */
//@Service
//@Slf4j
//public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
//    @Autowired
//    private ReservationMapper reservationMapper;
//
//    /**
//     * 新增预约信息
//     *
//     * @param dto 预约信息
//     * @return 结果
//     */
//    @Override
//    public void insertReservation(ReservationDto dto) {
//        Long userId = UserThreadLocal.getUserId();
//        Long count = lambdaQuery().eq(Reservation::getMobile, dto.getMobile())
//                .eq(Reservation::getTime, dto.getTime()).count();
//        if (count > 0) {
//            throw new ServiceException("该手机号码已经预约了这个时间段的了");
//        }
//        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
//        String userIdString = userId.toString();
//        reservation.setCreateBy(userIdString);
//        reservation.setUpdateBy(userIdString);
//        //未到访
//        reservation.setStatus(0);
//        save(reservation);
//    }
//
//
//    /**
//     * 取消预约信息信息
//     *
//     * @param id 预约信息主键
//     * @return 结果
//     */
//    @Override
//    public void cancelReservation(Long id) {
//        Reservation reservation = getById(id);
//        if (ObjUtil.isEmpty(reservation)) {
//            throw new BaseException("预约不存在");
//        }
//        reservation.setStatus(2);
//        reservation.setUpdateBy(String.valueOf(UserThreadLocal.getUserId()));
//        updateById(reservation);
//    }
//
//    /**
//     * 查询当天取消预约数量
//     *
//     * @return
//     */
//    @Override
//    public int cancelledCount() {
//        Long userId = UserThreadLocal.getUserId();
//        // 2024/10/16 00:00:00
//        LocalDateTime startTime = LocalDate.now().atStartOfDay();
//        // 2024/10/17 00:00:00
//        LocalDateTime endTime = startTime.plusDays(1);
//        LambdaQueryWrapper<Reservation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Reservation::getUpdateBy, userId);
//        lambdaQueryWrapper.between(Reservation::getUpdateTime, startTime, endTime);
//        //2代表取消预约
//        lambdaQueryWrapper.eq(Reservation::getStatus, 2);
//
//        return (int) count(lambdaQueryWrapper);
//    }
//
//    /**
//     * 查询每个时间段剩余预约次数
//     *
//     * @param time
//     * @return
//     */
//    @Override
//    public List<TimeCountVo> countByTime(Long time) {
//        LocalDateTime localDateTime = LocalDateTimeUtil.of(time);
//
//        LocalDateTime startTime = localDateTime.toLocalDate().atStartOfDay();
//
//        LocalDateTime endTime = startTime.plusDays(1);
//        List<TimeCountVo> timeCountVoList = reservationMapper.countReservationsForTime(startTime, endTime);
//        return timeCountVoList;
//    }
//
//    /**
//     * 分页查询预约信息
//     *
//     * @param pageNum
//     * @param pageSize
//     * @param status
//     * @return
//     */
//    @Override
//    public TableDataInfo<Reservation> selectByPage(int pageNum, int pageSize, Integer status) {
//        Long userId = UserThreadLocal.getUserId();
//        if (ObjUtil.isEmpty(userId)) {
//            throw new BaseException("请先登录");
//        }
//        LambdaQueryWrapper<Reservation> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Reservation::getCreateBy, userId);
//        lambdaQueryWrapper.eq(ObjUtil.isNotEmpty(status), Reservation::getStatus, status);
//        lambdaQueryWrapper.orderByDesc(Reservation::getCreateTime);
//        Page<Reservation> page = new Page<>(pageNum, pageSize);
//        page = page(page, lambdaQueryWrapper);
//        return new TableDataInfo<>(page.getTotal(), page.getRecords(), HttpStatus.SUCCESS, "请求成功");
//    }
//}
